﻿using Anke.SHManage.Model;
using Anke.SHManage.Model.BasicEventInfo;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Data.Linq;

namespace Anke.SHManage.MSSQLDAL
{
    public class TaskDAL
    {
        public static readonly SqlConnectionStringBuilder DispatchBuilder = new SqlConnectionStringBuilder(SqlHelper.AttemperConnectionString);//取调度库的链接字符串
        public static readonly string DispatchDatabase = DispatchBuilder.InitialCatalog;
        #region

        public object GetTaskList(int pageSize, int pageIndex, DateTime start, DateTime end, string center, string station, string actualNumber, string ambulanceNumber, string driver,
                                  string doctor, string nurse, string stretcher, string patientName, string callNumber, string contactTelephone, string localAddress, string sendAddress,
                                  E_StatisticsPermisson em, string selfWorkCode, string selfCenterID, string selfStationID, string eventDispatchCenterID)
        {
            List<TaskInfo> list = new List<TaskInfo>();
            StringBuilder sb = new StringBuilder();
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(SqlHelper.MainConnectionString);
            sb.Append(@"
select distinct identity(int,1,1) as 行号,事件编码=tae.事件编码,呼救电话=tac.呼救电话,现场地址=tac.现场地址,患者姓名=tac.患者姓名
,分中心=tc.名称,分站=ts.名称,车号=tal.实际标识,tt.生成任务时刻                    
,随车人员=tt.司机
+(case when tt.医生='' then '' else (',' +tt.医生) end)
+(case when tt.护士='' then '' else (',' +tt.护士) end)
+(case when tt.担架工='' then '' else (',' +tt.担架工) end)
+(case when tt.抢救员='' then '' else (',' +tt.抢救员) end)
,病历个数=(select count(*) from ");
            sb.Append(builder.InitialCatalog).Append(@".dbo.M_PatientRecord mpr where mpr.TaskCode=tt.任务编码) 
into #temp                   
from TTask tt 
left join TAcceptEvent tac on tac.事件编码=tt.事件编码 and tac.受理序号=tt.受理序号                     
left join TAlarmEvent tae on tae.事件编码 = tt.事件编码
left join TAmbulance tal on tal.车辆编码=tt.车辆编码
left join TStation ts on ts.编码=tt.分站编码
left join TCenter tc on tc.编码=tt.中心编码 ");
            if (em == E_StatisticsPermisson.SELF)
            {
                sb.Append(@"
left join TTaskPersonLink ttpl on ttpl.任务编码=tt.任务编码
left join TPerson tp on tp.编码=ttpl.人员编码");
            }
            sb.Append(@"                                    
where 1=1");
            WhereClauseUtility.AddDateTimeGreaterThan("tt.生成任务时刻", start, sb);
            WhereClauseUtility.AddDateTimeLessThan("tt.生成任务时刻", end, sb);
            sb.Append("  and tae.是否测试=0 ");
            WhereClauseUtility.AddInSelectQuery("tal.车辆编码", actualNumber, sb);
            WhereClauseUtility.AddStringLike("tal.车牌号码", ambulanceNumber, sb);
            WhereClauseUtility.AddStringLike("tac.患者姓名", patientName, sb);
            WhereClauseUtility.AddStringLike("tac.呼救电话", callNumber, sb);
            WhereClauseUtility.AddStringLike("tac.联系电话", contactTelephone, sb);
            WhereClauseUtility.AddStringLike("tac.现场地址", localAddress, sb);
            WhereClauseUtility.AddStringLike("tac.送往地点", sendAddress, sb);
            WhereClauseUtility.AddStringLike("tt.司机", driver, sb);
            WhereClauseUtility.AddStringLike("tt.医生", doctor, sb);
            WhereClauseUtility.AddStringLike("tt.护士", nurse, sb);
            WhereClauseUtility.AddStringLike("tt.担架工", stretcher, sb);
            WhereClauseUtility.AddStringEqual("tae.中心编码", eventDispatchCenterID, sb); //2020-11-22 zch
            if (em == E_StatisticsPermisson.None)
                return null;
            else
            {
                if (em == E_StatisticsPermisson.ALL)
                {
                    WhereClauseUtility.AddInSelectQuery("tt.中心编码", center, sb);
                    WhereClauseUtility.AddInSelectQuery("tt.分站编码", station, sb);
                }
                else if (em == E_StatisticsPermisson.CENTER)
                {
                    WhereClauseUtility.AddStringEqual("tt.中心编码", selfCenterID, sb);
                    WhereClauseUtility.AddInSelectQuery("tt.分站编码", station, sb);
                }
                else if (em == E_StatisticsPermisson.STATION)
                {
                    WhereClauseUtility.AddStringEqual("tt.分站编码", selfStationID, sb);
                }
                else if (em == E_StatisticsPermisson.SELF)
                {
                    sb.Append("and tp.工号 = '" + selfWorkCode + "' ");
                }
                sb.Append(@"
order by tt.生成任务时刻  desc

select top " + pageSize + " A.*  from #temp A where 行号>" + (pageIndex - 1) * pageSize + @" order by 行号 
SELECT count(*) FROM #temp t
drop table #temp ");
                DataSet ds = SqlHelper.ExecuteDataSet(SqlHelper.AttemperConnectionString, CommandType.Text, sb.ToString(), null);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        TaskInfo info = new TaskInfo();
                        info.callNumber = dr["呼救电话"].ToString();
                        info.localAdr = dr["现场地址"].ToString();
                        info.pName = dr["患者姓名"].ToString();
                        info.center = dr["分中心"].ToString();
                        info.station = dr["分站"].ToString();
                        info.actualNumber = dr["车号"].ToString();
                        info.driver = dr["随车人员"].ToString();
                        //info.driver = dr["司机"].ToString();
                        //info.doctor = dr["医生"].ToString();
                        //info.nurse = dr["护士"].ToString();
                        // info.stretcher = dr["担架工"].ToString();
                        info.taskGenerationTime = dr["生成任务时刻"].ToString();
                        info.patientRecordNumber = dr["病历个数"].ToString();
                        info.EventCode = dr["事件编码"].ToString();
                        list.Add(info);
                    }
                }
                int total = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                var result = new { total = total, rows = list };
                return result;

            }
        }
        private static Dictionary<string, int> m_ModifyTaskTypeDic = null;
        public string ModifyTask(TaskAmbulanceInfo info, string operatorCode)
        {
            try
            {
                using (SqlConnection con = new SqlConnection(SqlHelper.AttemperConnectionString))
                {

                    DataBase db = new DataBase(con);
                    TaskAmbulanceInfo originInfo = (from aei in db.TaskAmbulanceInfo
                                                    where aei.TaskCode == info.TaskCode
                                                    select aei).FirstOrDefault();
                    if (originInfo == null)
                        return "没有找到对应的任务实体";

                    #region 插入车辆状态时间表
                    //-------------------------插入车辆状态时间表---------------------
                    DateTime now = DateTime.Now;
                    int id = 1;
                    var query = from am in db.AmbulanceStateTimeInfo
                                select am.Code;
                    if (query.Count() > 0)
                        id = query.Max() + 1;
                    else
                        id = 1;
                    originInfo.GetCommandTime = originInfo.TGetCommandTime == null ? "" : Convert.ToDateTime(originInfo.TGetCommandTime).ToString("yyyy-MM-dd HH:mm:ss");
                    originInfo.OutAmbulanceTime = originInfo.TOutAmbulanceTime == null ? "" : Convert.ToDateTime(originInfo.TOutAmbulanceTime).ToString("yyyy-MM-dd HH:mm:ss");
                    originInfo.ArriveSenceTime = originInfo.TArriveSenceTime == null ? "" : Convert.ToDateTime(originInfo.TArriveSenceTime).ToString("yyyy-MM-dd HH:mm:ss");
                    originInfo.LeaveSenceTime = originInfo.TLeaveSenceTime == null ? "" : Convert.ToDateTime(originInfo.TLeaveSenceTime).ToString("yyyy-MM-dd HH:mm:ss");
                    originInfo.ArriveHospitalTime = originInfo.TArriveHospitalTime == null ? "" : Convert.ToDateTime(originInfo.TArriveHospitalTime).ToString("yyyy-MM-dd HH:mm:ss");
                    originInfo.MissionCompleteTime = originInfo.TMissionCompleteTime == null ? "" : Convert.ToDateTime(originInfo.TMissionCompleteTime).ToString("yyyy-MM-dd HH:mm:ss");
                    originInfo.ReturnStationTime = originInfo.TReturnStationTime == null ? "" : Convert.ToDateTime(originInfo.TReturnStationTime).ToString("yyyy-MM-dd HH:mm:ss");
                    info.GetCommandTime = string.IsNullOrEmpty(info.GetCommandTime) ? "" : Convert.ToDateTime(info.GetCommandTime).ToString("yyyy-MM-dd HH:mm:ss");
                    info.OutAmbulanceTime = string.IsNullOrEmpty(info.OutAmbulanceTime) ? "" : Convert.ToDateTime(info.OutAmbulanceTime).ToString("yyyy-MM-dd HH:mm:ss");
                    info.ArriveSenceTime = string.IsNullOrEmpty(info.ArriveSenceTime) ? "" : Convert.ToDateTime(info.ArriveSenceTime).ToString("yyyy-MM-dd HH:mm:ss");
                    info.LeaveSenceTime = string.IsNullOrEmpty(info.LeaveSenceTime) ? "" : Convert.ToDateTime(info.LeaveSenceTime).ToString("yyyy-MM-dd HH:mm:ss");
                    info.ArriveHospitalTime = string.IsNullOrEmpty(info.ArriveHospitalTime) ? "" : Convert.ToDateTime(info.ArriveHospitalTime).ToString("yyyy-MM-dd HH:mm:ss");
                    info.MissionCompleteTime = string.IsNullOrEmpty(info.MissionCompleteTime) ? "" : Convert.ToDateTime(info.MissionCompleteTime).ToString("yyyy-MM-dd HH:mm:ss");
                    info.ReturnStationTime = string.IsNullOrEmpty(info.ReturnStationTime) ? "" : Convert.ToDateTime(info.ReturnStationTime).ToString("yyyy-MM-dd HH:mm:ss");
                    if (originInfo.GetCommandTime != info.GetCommandTime)
                    {
                        AmbulanceStateTimeInfo asInfo = new AmbulanceStateTimeInfo();
                        asInfo.WorkStateId = 1;
                        asInfo.KeyPressTime = info.GetCommandTime == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(info.GetCommandTime);
                        asInfo.Code = id;
                        asInfo.AmbuCode = info.AmbulanceCode;
                        asInfo.TaskCode = info.TaskCode;
                        asInfo.SaveTime = now;
                        asInfo.SourceCode = 5;
                        asInfo.OperationCode = operatorCode;
                        asInfo.IsOnline = true;
                        db.AmbulanceStateTimeInfo.InsertOnSubmit(asInfo);
                        id++;
                    }
                    if (originInfo.OutAmbulanceTime != info.OutAmbulanceTime)
                    {
                        AmbulanceStateTimeInfo asInfo = new AmbulanceStateTimeInfo();
                        asInfo.WorkStateId = 2;
                        asInfo.KeyPressTime = info.OutAmbulanceTime == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(info.OutAmbulanceTime);
                        asInfo.Code = id;
                        asInfo.AmbuCode = info.AmbulanceCode;
                        asInfo.TaskCode = info.TaskCode;
                        asInfo.SaveTime = now;
                        asInfo.SourceCode = 5;
                        asInfo.OperationCode = operatorCode;
                        asInfo.IsOnline = true;
                        db.AmbulanceStateTimeInfo.InsertOnSubmit(asInfo);
                        id++;
                    }
                    if (originInfo.ArriveSenceTime != info.ArriveSenceTime)
                    {
                        AmbulanceStateTimeInfo asInfo = new AmbulanceStateTimeInfo();
                        asInfo.WorkStateId = 3;
                        asInfo.KeyPressTime = info.ArriveSenceTime == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(info.ArriveSenceTime);
                        asInfo.Code = id;
                        asInfo.AmbuCode = info.AmbulanceCode;
                        asInfo.TaskCode = info.TaskCode;
                        asInfo.SaveTime = now;
                        asInfo.SourceCode = 5;
                        asInfo.OperationCode = operatorCode;
                        asInfo.IsOnline = true;
                        db.AmbulanceStateTimeInfo.InsertOnSubmit(asInfo);
                        id++;
                    }
                    if (originInfo.LeaveSenceTime != info.LeaveSenceTime)
                    {
                        AmbulanceStateTimeInfo asInfo = new AmbulanceStateTimeInfo();
                        asInfo.WorkStateId = 4;
                        asInfo.KeyPressTime = info.LeaveSenceTime == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(info.LeaveSenceTime);
                        asInfo.Code = id;
                        asInfo.AmbuCode = info.AmbulanceCode;
                        asInfo.TaskCode = info.TaskCode;
                        asInfo.SaveTime = now;
                        asInfo.SourceCode = 5;
                        asInfo.OperationCode = operatorCode;
                        asInfo.IsOnline = true;
                        db.AmbulanceStateTimeInfo.InsertOnSubmit(asInfo);
                        id++;
                    }
                    if (originInfo.ArriveHospitalTime != info.ArriveHospitalTime)
                    {
                        AmbulanceStateTimeInfo asInfo = new AmbulanceStateTimeInfo();
                        asInfo.WorkStateId = 5;
                        asInfo.KeyPressTime = info.ArriveHospitalTime == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(info.ArriveHospitalTime);
                        asInfo.Code = id;
                        asInfo.AmbuCode = info.AmbulanceCode;
                        asInfo.TaskCode = info.TaskCode;
                        asInfo.SaveTime = now;
                        asInfo.SourceCode = 5;
                        asInfo.OperationCode = operatorCode;
                        asInfo.IsOnline = true;
                        db.AmbulanceStateTimeInfo.InsertOnSubmit(asInfo);
                        id++;
                    }
                    if (originInfo.MissionCompleteTime != info.MissionCompleteTime)
                    {
                        AmbulanceStateTimeInfo asInfo = new AmbulanceStateTimeInfo();
                        asInfo.WorkStateId = 6;
                        asInfo.KeyPressTime = info.MissionCompleteTime == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(info.MissionCompleteTime);
                        asInfo.Code = id;
                        asInfo.AmbuCode = info.AmbulanceCode;
                        asInfo.TaskCode = info.TaskCode;
                        asInfo.SaveTime = now;
                        asInfo.SourceCode = 5;
                        asInfo.OperationCode = operatorCode;
                        asInfo.IsOnline = true;
                        db.AmbulanceStateTimeInfo.InsertOnSubmit(asInfo);
                        id++;
                    }
                    if (originInfo.ReturnStationTime != info.ReturnStationTime)
                    {
                        AmbulanceStateTimeInfo asInfo = new AmbulanceStateTimeInfo();
                        asInfo.WorkStateId = 7;
                        asInfo.KeyPressTime = info.ReturnStationTime == "" ? Convert.ToDateTime("1900-01-01") : Convert.ToDateTime(info.ReturnStationTime);
                        asInfo.Code = id;
                        asInfo.AmbuCode = info.AmbulanceCode;
                        asInfo.TaskCode = info.TaskCode;
                        asInfo.SaveTime = now;
                        asInfo.SourceCode = 5;
                        asInfo.OperationCode = operatorCode;
                        asInfo.IsOnline = true;
                        db.AmbulanceStateTimeInfo.InsertOnSubmit(asInfo);
                        id++;
                    }
                    #endregion

                    #region 插入出车人员表
                    //----------------------插入出车人员表------------------------------
                    //先删除出车人员
                    var oldQuery = from t in db.TaskPersonLinkInfo
                                   where t.TaskCode == info.TaskCode
                                   select t;
                    db.TaskPersonLinkInfo.DeleteAllOnSubmit(oldQuery);
                    //再插入出车人员
                    List<string> LtDriver = StringToList(info.DriverCodes);
                    List<string> LtDovtor = StringToList(info.DoctorCodes);
                    List<string> LtNures = StringToList(info.NurseCodes);
                    List<string> LtLitter = StringToList(info.LitterCodes);
                    List<string> LtRescue = StringToList(info.RescueCodes);
                    List<string> list = new List<string>();
                    if (LtDriver != null && LtDriver.Count > 0)
                    {
                        foreach (string item in LtDriver)
                        { list.Add(item); }
                    }
                    if (LtDovtor != null && LtDovtor.Count > 0)
                    {
                        foreach (string item in LtDovtor)
                        { list.Add(item); }
                    }
                    if (LtNures != null && LtNures.Count > 0)
                    {
                        foreach (string item in LtNures)
                        { list.Add(item); }
                    }
                    if (LtLitter != null && LtLitter.Count > 0)
                    {
                        foreach (string item in LtLitter)
                        { list.Add(item); }
                    }
                    if (LtRescue != null && LtRescue.Count > 0)
                    {
                        foreach (string item in LtRescue)
                        { list.Add(item); }
                    }
                    TaskPersonLinkInfo ttplInfo;
                    foreach (string item in list)
                    {
                        ttplInfo = new TaskPersonLinkInfo();
                        PersonInfo perInfo = (from p in db.PersonInfo
                                              where p.Code == item
                                              select p).FirstOrDefault();
                        ttplInfo.TaskCode = info.TaskCode;
                        ttplInfo.Code = item;
                        ttplInfo.Name = perInfo.Name;
                        ttplInfo.PersonTypeCode = perInfo.PersonTypeCode;
                        ttplInfo.StationCode = perInfo.StationCode;
                        ttplInfo.Valid = perInfo.Valid;
                        db.TaskPersonLinkInfo.InsertOnSubmit(ttplInfo);
                    }
                    #endregion

                    #region 更新主表
                    m_ModifyTaskTypeDic = new Dictionary<string, int>();
                    m_ModifyTaskTypeDic.Add("IFNormalEnd", 113);//任务是否正常结束
                    m_ModifyTaskTypeDic.Add("ResultReason", 114);//异常结束原因
                    m_ModifyTaskTypeDic.Add("TaskRemark", 115);//任务备注
                    m_ModifyTaskTypeDic.Add("DriveKM", 116);//行驶公里数
                    m_ModifyTaskTypeDic.Add("EmergencyKM", 117);//急救公里数
                    m_ModifyTaskTypeDic.Add("PatientCount", 118);//实际救治人数

                    m_ModifyTaskTypeDic.Add("AmbulanceCode", 101);//车辆编码
                    m_ModifyTaskTypeDic.Add("StationCode", 119);//分站编码
                    m_ModifyTaskTypeDic.Add("Driver", 102);//司机
                    m_ModifyTaskTypeDic.Add("Doctor", 103);//医生
                    m_ModifyTaskTypeDic.Add("Nurse", 104);//护士
                    m_ModifyTaskTypeDic.Add("DanJiaGong", 105);//担架员
                    m_ModifyTaskTypeDic.Add("Rescue", 120);//抢救员

                    m_ModifyTaskTypeDic.Add("GetCommandTime", 106);//收到指令
                    m_ModifyTaskTypeDic.Add("OutAmbulanceTime", 107);//驶向现场
                    m_ModifyTaskTypeDic.Add("ArriveSenceTime", 108);//抢救转送
                    m_ModifyTaskTypeDic.Add("LeaveSenceTime", 109);//病人上车
                    m_ModifyTaskTypeDic.Add("ArriveHospitalTime", 110);//到达医院
                    m_ModifyTaskTypeDic.Add("MissionCompleteTime", 111);//途中待命
                    m_ModifyTaskTypeDic.Add("ReturnStationTime", 112);//站内待命
                    List<ModifyRecordInfo> mriList = new List<ModifyRecordInfo>();
                    //获得所有property的信息
                    PropertyInfo[] properties = originInfo.GetType().GetProperties();
                    foreach (PropertyInfo p in properties)
                    {
                        int typeId = -1;
                        if (m_ModifyTaskTypeDic.TryGetValue(p.Name, out typeId)) //如果需要修改
                        {
                            object oldObj = p.GetValue(originInfo, null);
                            object newObj = p.GetValue(info, null);
                            string oldValue = oldObj == null ? "" : oldObj.ToString();
                            string newValue = newObj == null ? "" : newObj.ToString();
                            if (!object.Equals(oldValue, newValue))
                            {
                                p.SetValue(originInfo, newObj, null); //给属性赋新值
                                ModifyRecordInfo mri = new ModifyRecordInfo();
                                mri.Code = Guid.NewGuid();
                                mri.NewContent = newObj.ToString();//2011-02-14 如果是null就费了。
                                mri.OriginContent = oldObj.ToString();//2011-02-14 如果是null就费了。
                                mri.OperateTime = DateTime.Now;
                                mri.OperatorCode = operatorCode;
                                mri.TypeId = typeId;
                                mri.EventCode = originInfo.EventCode;
                                mri.AcceptOrder = originInfo.AcceptNumber;
                                mri.TaskCode = originInfo.TaskCode;//任务编码
                                mriList.Add(mri);
                            }
                        }
                    }
                    db.ModifyRecordInfos.InsertAllOnSubmit(mriList);
                    //因为页面传参实体类和后台查询实体的状态时刻属性名不同（如TGetCommandTime和GetCommandTime）
                    //所以没法区别值的不同，所以需要单独赋值和单独记录痕迹（bug：查看痕迹时TModifyRecord表就查不到啦啊）
                    originInfo.TGetCommandTime = string.IsNullOrEmpty(info.GetCommandTime) ? (DateTime?)null : Convert.ToDateTime(info.GetCommandTime);
                    originInfo.TOutAmbulanceTime = string.IsNullOrEmpty(info.OutAmbulanceTime) ? (DateTime?)null : Convert.ToDateTime(info.OutAmbulanceTime);
                    originInfo.TArriveSenceTime = string.IsNullOrEmpty(info.ArriveSenceTime) ? (DateTime?)null : Convert.ToDateTime(info.ArriveSenceTime);
                    originInfo.TLeaveSenceTime = string.IsNullOrEmpty(info.LeaveSenceTime) ? (DateTime?)null : Convert.ToDateTime(info.LeaveSenceTime);
                    originInfo.TArriveHospitalTime = string.IsNullOrEmpty(info.ArriveHospitalTime) ? (DateTime?)null : Convert.ToDateTime(info.ArriveHospitalTime);
                    originInfo.TMissionCompleteTime = string.IsNullOrEmpty(info.MissionCompleteTime) ? (DateTime?)null : Convert.ToDateTime(info.MissionCompleteTime);
                    originInfo.TReturnStationTime = string.IsNullOrEmpty(info.ReturnStationTime) ? (DateTime?)null : Convert.ToDateTime(info.ReturnStationTime);
                    #endregion
                    db.SubmitChanges();
                    return "";
                }
            }
            catch (Exception ex)
            { return ex.ToString(); }
        }
        private static List<string> StringToList(string spStr)
        {
            List<string> list = null;
            if (!string.IsNullOrEmpty(spStr))
            {
                list = new List<string>();
                string[] array = spStr.Split('|');
                if (array.Length == 1 && array[0] == "")
                { }
                else
                {
                    foreach (string r in array)
                    {
                        list.Add(Convert.ToString(r));
                    }
                }
            }
            return list;
        }
        #endregion

        /// <summary>
        /// 查询修改痕迹
        /// 20160926 刘爱青 add
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowCount"></param>
        /// <param name="TaskCode"></param>
        /// <returns></returns>
        public List<ModifyRecordInfo> GetCrackInfos(int pageIndex, int pageSize, ref int rowCount, string TaskCode)
        {
            DataBase db = new DataBase(SqlHelper.AttemperConnectionString);

            var qry = (from alm in db.ModifyRecordInfos
                       join m in db.ModifyRecordTypeInfo on alm.TypeId equals m.BianMa into temp
                       join person in db.PersonInfo on alm.OperatorCode equals person.Code into tempPerson
                       from tt in temp.DefaultIfEmpty()
                       from p in tempPerson.DefaultIfEmpty()
                       where alm.TaskCode == TaskCode
                       select new
                      {
                          Code = alm.Code,
                          TaskCode = alm.TaskCode,
                          OperatorName = p == null ? "" : p.Name,//这里主要第二个集合有可能为空。需要判断
                          OperateTime = alm.OperateTime,
                          StringOperateTime = alm.OperateTime.ToString(),
                          TypeName = tt == null ? "" : tt.MingCheng,//这里主要第二个集合有可能为空。需要判断
                          OriginContent = alm.OriginContent,
                          NewContent = alm.NewContent
                      }).ToList();
            List<ModifyRecordInfo> list = new List<ModifyRecordInfo>();
            ModifyRecordInfo info = new ModifyRecordInfo();
            for (int i = 0; i < qry.Count; i++)
            {
                info = new ModifyRecordInfo();
                info.Code = (Guid)qry[i].Code;
                info.TaskCode = qry[i].TaskCode.ToString();
                info.OperatorName = qry[i].OperatorName.ToString();
                info.OperateTime = Convert.ToDateTime(qry[i].OperateTime);
                info.StringOperateTime = Convert.ToDateTime(qry[i].StringOperateTime).ToString("yyyy-MM-dd HH:mm:ss");
                info.TypeName = qry[i].TypeName.ToString();
                info.OriginContent = qry[i].OriginContent.ToString();
                info.NewContent = qry[i].NewContent.ToString();
                list.Add(info);
            }
            list = list.OrderByDescending(t => t.OperateTime).ToList();
            rowCount = qry.Count;
            return list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
        }
        /// <summary>
        /// 根据工号查询人员信息
        /// </summary>
        /// <param name="workID"></param>
        /// <returns></returns>
        public PersonInfo GetPreson(string workID)
        {
            DataBase db = new DataBase(SqlHelper.AttemperConnectionString);
            PersonInfo info = (from a in db.PersonInfo
                               where a.JobCode == workID
                               select a).FirstOrDefault();
            return info;
        }
    }
}
